
import java.util.ArrayList;
import java.util.List;

import com.badlogic.gdx.Input;
import com.constant.GameConstant;


public class GuiElementManager {
	private static GuiElementManager m_instance = null;
	List<GuiElementAbstract> m_list = new ArrayList<GuiElementAbstract>();
	private boolean m_isTouched=false;
	private GuiElementManager(){};	
	public final static boolean MODE_EDIT=false;

	public static GuiElementManager getInstance()
	{
		if(m_instance ==null)
		{
			m_instance = new GuiElementManager();
		}
		return m_instance;
	}
	
	public void addElement(GuiElementAbstract guielement)
	{
		m_list.add(guielement);
	}
	
	public void removeElement(GuiElementAbstract guielement)
	{
		m_list.remove(guielement);
	}
	
	public void checkIntersect(int posx,int posy)
	{
		int len = m_list.size();
		for (int i = 0; i < len; i++) 
		{
			GuiElementAbstract element = m_list.get(i);
			
			if(!element.isIntersect(posx, posy))
			{				
				element.justTouchedReleased();
			}
		}
	}
	/*
	 *  call this when touch have been released, check which gui is touched
	 */
	public int checkClicked(int posx,int posy)
	{
		// System.out.println("__X: "+posx);
		// System.out.println("__Y: "+posy);
		int retVal=-1;
		int len = m_list.size();
		for (int i = 0; i < len; i++) 
		{
			GuiElementAbstract element = m_list.get(i);
			element.justTouchedReleased();
			if(element.isIntersect(posx, posy))
			{				
				retVal= element.getGuiID();
			}
		}
		return retVal;
	}
	/*
	 *  call this when first touch happen, event for changing gui to touched-state
	 */
	public void justTouched(int posx,int posy)
	{
		int len = m_list.size();
		for (int i = 0; i < len; i++) 
		{
			GuiElementAbstract element = m_list.get(i);
			if(element.isIntersect(posx, posy))
			{
				element.justTouched();
			}
		}
	}
	
	public int update(Input input)
	{
		if(input.justTouched())
		{
			GuiElementManager.getInstance().justTouched(input.getX(), GameConstant.H_SCREEN - input.getY());
			m_isTouched = true;
			
			System.out.println("just touched");
		}
		if(m_isTouched)
		{
			if( !input.isTouched() ) // if touch is already released)
			{
				System.out.println("released touched");
				m_isTouched =false;
				int pix = input.getX();
				int piy = input.getY();
				if(MODE_EDIT)
				{
					// save
					return -1;
				}
				else
					return GuiElementManager.getInstance().checkClicked(pix, GameConstant.H_SCREEN - piy); 
			}
			else // if touch is not yet released
			{
				//--if debug
				if(MODE_EDIT) {
					System.out.println("touch is not yet released");
					int len = m_list.size();
					for (int i = 0; i < len; i++) 
					{
						GuiElementAbstract element = m_list.get(i);
						if(element.isIntersect(input.getX(), GameConstant.H_SCREEN - input.getY()))
						{				
							int element_width  = element.getWidth();
							int element_height = element.getHeight();
							int _px = input.getX() -(element_width/2);
							int _py = GameConstant.H_SCREEN -input.getY() ;
							System.out.println("x: "+_px);
							System.out.println("y: "+_py);
							element.setPos(input.getX() -(element_width/2), GameConstant.H_SCREEN -input.getY() );
						}
					}				
				}
				//--end if debug
			}
		}
		return -1;
	}
	public void render() 
	{
		int len = m_list.size();
		for (int i = 0; i < len; i++) 
		{
			GuiElementAbstract element = m_list.get(i);
			element.render();
		}
	}
}
